package com.kudian.restful.controller;

import com.kudian.common.persistence.ResultObj;
import com.kudian.common.utils.DateUtils;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.entity.Collection;
import com.kudian.restful.entity.LoginSession;
import com.kudian.restful.service.AdService;
import com.kudian.restful.service.CollectionService;
import com.kudian.restful.service.LoginSessionService;
import com.kudian.restful.vo.ad.CarouselRetVO;
import com.kudian.restful.vo.ad.MidAdRetVO;
import com.kudian.restful.vo.ad.MidAdVO;
import com.kudian.restful.vo.collection.*;
import com.kudian.restful.vo.seller.AddSellerRetVO;
import com.kudian.restful.vo.seller.AddSellerVO;
import com.kudian.restful.vo.welinfo.WelInfoPageRetVO;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2016/4/27.
 */
@RestController
@RequestMapping("collection")
public class CollectionController {

    private static final Logger logger = Logger.getLogger(CollectionController.class);

    @Autowired
    private LoginSessionService loginSessionService;

    @Autowired
    private CollectionService collectionService;

    @RequestMapping(value = "add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "添加收藏", httpMethod = "POST", notes = "添加收藏", response = AddCollectionRetVO.class)
    public AddCollectionRetVO add(@RequestBody AddCollectionVO vo) {
        AddCollectionRetVO o = new AddCollectionRetVO();

        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("收藏失败!access_token为空");
            return o;
        }
        LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }

        try {
            Collection collection = new Collection();
            collection.setUserId(ent.getUserId());
            collection.setSellerId(vo.getSellerId());
            collection.setGoodsId(vo.getGoodsId());
            collection.setAddTime(DateUtils.getSysTimestamp());
            this.collectionService.save(collection);
            o.setErrcode(0);
            o.setErrmsg("收藏成功!");
            o.setCollectionId(collection.getCollectionId());
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("收藏失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "queryCollectionSellers", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询收藏的店铺", httpMethod = "POST", notes = "查询收藏的店铺", response = QueryCollectionSellersRetVO.class)
    public QueryCollectionSellersRetVO queryCollectionSellers(@RequestBody QueryCollectionVO vo) {
        QueryCollectionSellersRetVO o = new QueryCollectionSellersRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("查询收藏的店铺失败!access_token为空");
            return o;
        }
        LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }

        try {
            o.setItems(this.collectionService.queryCollectionSellers(vo));
            o.setErrcode(0);
            o.setErrmsg("查询收藏的店铺成功!");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("查询收藏的店铺失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "queryCollectionGoods", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询收藏的商品", httpMethod = "POST", notes = "查询收藏的商品", response = QueryCollectionSellersRetVO.class)
    public QueryCollectionGoodsRetVO queryCollectionGoods(@RequestBody QueryCollectionVO vo) {
        QueryCollectionGoodsRetVO o = new QueryCollectionGoodsRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("查询收藏的店铺失败!access_token为空");
            return o;
        }
        LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }

        try {
            o.setItems(this.collectionService.queryCollectionGoods(vo));
            o.setErrcode(0);
            o.setErrmsg("查询收藏的店铺成功!");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("查询收藏的店铺失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除收藏", httpMethod = "POST", notes = "删除收藏", response = QueryCollectionSellersRetVO.class)
    public ResultObj delete(@RequestBody DeleteCollectionVO vo) {
        ResultObj o = new ResultObj();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("删除收藏失败!access_token为空");
            return o;
        }
        LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }

        try {
            collectionService.delete(vo.getCollectionId());
            o.setErrcode(0);
            o.setErrmsg("删除收藏成功!");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("删除收藏失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }
}
